package com.probiz.estore.imports.handler.customer;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.model.customer.Membership;
import com.probiz.estore.customer.service.MembershipManager;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;

public class MembershipHandler extends ColumnBasicHandler implements ColumnHandler {

	private MembershipManager membershipManager;

	public void setMembershipManager(MembershipManager membershipManager) {
		this.membershipManager = membershipManager;
	}

	public void setProperty(ImportModel importModel, Column column)	throws Exception {
		Customer customer = (Customer)importModel.getTarget();
		String value = column.getValue();
		//为空的话如果是新增则设置为默认等级，如果是修改则不修改他的等级;无论新增还是修改有填写会员等级但会员等级找不到的话 就无法导入；
		if(StringUtils.isBlank(value)){
			if(customer.getMembership()==null){
				//case:原来的会员等级为空，则设置为默认会员等级
				customer.setMembership(membershipManager.getBaseMembership());
			}else{
				//case:原来的会员等级不为空，则不做任何处理
			}
		}else{
			List<Membership> membershipList = membershipManager.getMembershipByName(value);
			if(membershipList.size() > 0){
				//case：有多条同名的会员等级,则设置为第一条
				customer.setMembership(membershipList.get(0));
			}else{
				//case:找不到会员等级
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "找不到会员等级("+value+"),无法导入";
				importModel.setMsg(msg);
				return;
			}
		}
		
		
		//设置结果
		importModel.setResult(ImportModel.RESULT_SUCCESS);
		
	}

}
